<template>
	<view class="mainpadding beijingse">
		<view class="mainpadding ffffff radius">
			<input type="number" placeholder="请输入手机号" v-model="mobile">
		</view>
		<view class="mainpadding ffffff radius margin_top flexbetween">
			<input class="input" type="number" placeholder="请输入验证码" v-model="captcha">
			<view class="huangsetext" @click="sendcode">{{codetext}}</view>
		</view>
		<view class="mainpadding ffffff radius margin_top">
			<input type="number" placeholder="请输入新密码" v-model="newpassword">
		</view>
		<view class="mainpadding ffffff radius margin_top">
			<input type="number" placeholder="请再次输入新密码" v-model="newpassword1">
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb" @click="ljxg">
			<view class="bigbtn">立即修改</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				codetext: "获取验证码",
				mobile: "",
				newpassword: "",
				captcha: "",
				newpassword1:""
			}
		},
		methods: {
			// 发送验证码
			sendcode() {
				let _this = this
				let mobile = _this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: mobile,
					event: "changepwd"
				}).then(resp => {
					if (resp.code == 1) {
						var i = 60;
						var timer = setInterval(function() {
							_this.codetext = i + "秒";
							if (i < 1) {
								clearInterval(timer);
								_this.codetext = "获取验证码";
							} else {
								i--;
							}
						}, 1000)

					} else if (resp.code == 0) {
						httpRequest.toast(resp.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				})
			},
			// 修改密码
			ljxg() {
				let _this = this
				let mobile = _this.mobile;
				if (mobile == '') {
					httpRequest.toast('请输入手机号');
					return false;
				}
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if (_this.captcha == '') {
					httpRequest.toast("请输入验证码");
					return false;
				}
				if (_this.newpassword == '') {
					httpRequest.toast("请输入密码");
					return false;
				}
				if (_this.newpassword1 == '') {
					httpRequest.toast("请输入确认密码");
					return false;
				}
				if (_this.newpassword != _this.newpassword1) {
					httpRequest.toast("两次输入密码不一致,请重新输入")
					return false
				}
				httpRequest.request('/api/user/changepwd', 'POST', {
					mobile: _this.mobile,
					newpassword: _this.newpassword,
					captcha: _this.captcha,
				}).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg);
						setTimeout(function() {
							uni.navigateBack({
								delta: 2
							});
						}, 2000)
					} else if (res.code == 0) {
						httpRequest.toast(res.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.huangsetext {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FAB536;
	}

	.input {
		width: 60%;
	}
</style>
